#ifndef POLHEMUS_TRACKER_H
#define POLHEMUS_TRACKER_H

/* 
    PolhemusTracker.h
	Author: Gabriel Baud-Bovy
*/

#ifdef _WINDOWS
#include <tchar.h> // needed to define _T macro used in PDI_90/Inc/PDI.h (VC0 version of PDI.h)
#include "PDI.h"
#endif

#include "Tracker.h"
#include "TrackerBuffer.h"
#include "PolhemusFrame.h"

#define	POLErrChk(functionCall) if(!(functionCall)) { strncpy(errorMsg,pdiDev.GetLastResultStr(),POL_ERROR_MSG_SIZE); return FALSE;}
#define	POLAllocChk(functionCall) if((functionCall)==NULL) { strcpy(errorMsg,"POL - Memory Allocation Error\n"); return FALSE; }

class PolhemusTracker : public Tracker {
public: 

	PolhemusTracker(void); 
	~PolhemusTracker(void);

	int open(void);
	int close(void);


	// get position and orientation 
	int poll(float *data);
	

	// Continuous Acqusition
	int	setRate(float r);
	int getRate(float *r);

	int start(void);
	int stop(void);	
	int sync(void); 


	// Read frames coming from the polhemus tracker and store them in an internal
	// circular buffer. This function needs to be called regularly to avoid overwrite
	// in Polhemus buffer which is very limited in size. 
	int read(void);

	// returns the number of frames that have not yet been read
	int  getNumFramesAvailable(void);

	// update internal pointer to the next unread pointer
	// return false if there are no available frame.
	int  nextFrame(void);

	// get position and orientation for of the last acquired frame
//	int getLastestData(int sensor, int channel, float &value); 
	int getData(int sensor, int channel, float &value);

	char *getErrorInfo(void);
private: 

	// Function to open, configure and close Polhemus tracker
	int	connect(void);
	int setupDevice(int sample);
	int disconnect(void);

	void parseErrorCode(unsigned char error); 

	// The PolhemusFrame class help with decoding the frame from Polhemus P&O buffer
	PolhemusFrame	frame;

	// The TrackerBuffer class provides a circular buffer of float with two pointers
	// to insert and retreiv data respectively
	TrackerBuffer tb;

#ifdef _WINDOWS
	// The CPDIdev class provides the control interface for managing a Polhemus tracker device.  
	// An instantiation of CPDIdev represents a single connection to a single tracker. 
	CPDIdev		pdiDev; 
	// The CPDImdat class is used for describing the programmable output data contained in motion 
	// frames received from the tracker. 
	CPDImdat    pdiMDat;
	// The CPDIser class is used to configure serial port communication with the tracker
	CPDIser		pdiSer;

	ePiCommType eType;

#endif

	int	bCnxReady;

	// client-supplied internal circular buffer for Polhemus PDI API 
	// to store data from the tracker	
	unsigned char	*buffer; 
	int	numFrames;
	int	bufferSize;		// size of the internal buffer
	int	bufferLastRead;	// position of the last element of the internal buffer read

	// count the number of frames read from the internal buffer 
	unsigned long frameCount;

	char errorMsg[POL_ERROR_MSG_SIZE];	// buffer for messages

//	ePiFrameRate rate;			// sampling rate 


	int CheckBIT(void);

};


#endif /* POLHEMUS_TRACKER_H */

